import 'dart:math';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

class LineChartResidual extends StatefulWidget {
  const LineChartResidual({super.key, this.isShowXTitle = true});

  final bool isShowXTitle;

  @override
  State<LineChartResidual> createState() => _LineChartResidualState();
}

class _LineChartResidualState extends State<LineChartResidual> {
  List<Color> gradientColors = [
    Colors.white,
    Colors.blue,
  ];

  bool showAvg = false;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        AspectRatio(
          aspectRatio: 1.70,
          child: Padding(
            padding: const EdgeInsets.only(
              top: 10,
            ),
            child: LineChart(
              mainData(),
            ),
          ),
        ),
        // SizedBox(
        //   width: 60,
        //   height: 34,
        //   child: TextButton(
        //     onPressed: () {
        //       setState(() {
        //         showAvg = !showAvg;
        //       });
        //     },
        //     child: Text(
        //       'avg',
        //       style: TextStyle(
        //         fontSize: 12,
        //         color: Colors.white,
        //       ),
        //     ),
        //   ),
        // ),
      ],
    );
  }

  Widget bottomTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
      fontWeight: FontWeight.bold,
      fontSize: 11,
      color: Color(0xFF65CEFF),
    );
    Widget text;
    switch (value.toInt()) {
      case 0:
        text = const Text('07/12', style: style);
        break;
      case 20:
        text = const Text('08/13', style: style);
        break;
      case 40:
        text = const Text('09/14', style: style);
        break;
      case 60:
        text = const Text('10/15', style: style);
        break;
      case 80:
        text = const Text('10/15', style: style);
        break;
      case 100:
        text = const Text('10/15', style: style);
        break;
      default:
        text = const Text('', style: style);
        break;
    }

    return SideTitleWidget(
      axisSide: meta.axisSide,
      child: widget.isShowXTitle ? text : Text(""),
    );
  }

  Widget leftTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
      fontSize: 11,
      color: Colors.grey,
    );
    String text;
    switch (value.toInt()) {
      case 0:
        text = '0%';
        break;
      case 5:
        text = '5%';
        break;
      case 10:
        text = '10%';
        break;
      case 15:
        text = '15%';
        break;
      case 20:
        text = '20%';
        break;
      case 25:
        text = '25%';
        break;
      case 30:
        text = '30%';
        break;
      default:
        return Container();
    }

    return Text(text, style: style, textAlign: TextAlign.left);
  }

  LineChartData mainData() {
    return LineChartData(
      gridData: FlGridData(
        show: true,
        drawVerticalLine: true,
        horizontalInterval: 1,
        verticalInterval: 1,
        getDrawingHorizontalLine: (value) {
          return const FlLine(
            color: Colors.grey,
            strokeWidth: 0,
          );
        },
        getDrawingVerticalLine: (value) {
          return const FlLine(
            color: Colors.grey,
            strokeWidth: 0,
          );
        },
      ),
      titlesData: FlTitlesData(
        show: true,
        rightTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        topTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        bottomTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            reservedSize: 30,
            interval: 1,
            getTitlesWidget: bottomTitleWidgets,
          ),
        ),
        leftTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            interval: 1,
            getTitlesWidget: leftTitleWidgets,
            reservedSize: 42,
          ),
        ),
      ),
      borderData: FlBorderData(
        show: false, // 隐藏边框--外边框
        border: Border.all(color: const Color(0xff37434d)),
      ),
      minX: 0,
      maxX: 100,
      minY: 0,
      maxY: 30,
      lineBarsData: [
        LineChartBarData(
          spots: const [
            FlSpot(0, 20),
            FlSpot(20, 22),
            FlSpot(40, 15),
            FlSpot(60, 23),
            FlSpot(80, 14),
            FlSpot(100, 3),
          ],
          isCurved: true,
          gradient: LinearGradient(
            colors: [Colors.blue, Colors.blue],
          ),
          barWidth: 1,

          /// 顶部线条宽度
          isStrokeCapRound: true,
          dotData: const FlDotData(
            show: true,
          ),
        ),
        LineChartBarData(
          spots: const [
            FlSpot(0, 13),
            FlSpot(20, 25),
            FlSpot(40, 22),
            FlSpot(60, 11),
            FlSpot(80, 18),
            FlSpot(100, 2),
          ],
          isCurved: true,
          gradient: LinearGradient(
            colors: [Colors.orange, Colors.orange],
          ),
          barWidth: 1,

          /// 顶部线条宽度
          isStrokeCapRound: true,
          dotData: const FlDotData(
            show: true,
          ),
        ),
      ],
    );
  }
}
